package edu.cicese.tm;

import com.sun.awt.AWTUtilities;
import edu.cicese.graphics.DashBorder;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 16/03/11
 * Time: 04:43 PM
 */
public class DialogLoadingImages extends JDialog implements Runnable{

	private FrameSpace frmSpace;
	public JProgressBar pbProgress;

	public double entryPerc;
	private int entriesCreated = 0;
	private File[] files;

	public DialogLoadingImages(FrameSpace owner, File[] files) {
		super(owner);
		this.frmSpace = owner;
		this.files = files;
		initComponents();
	}

	//! Creates the GUI components
	public void initComponents() {
		setUndecorated(true);
		setSize(220, 51);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle window = getBounds();
		setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);

		JPanel pProgress = new JPanel();
		pProgress.setLayout(null);
		pProgress.setBorder(new LineBorder(new Color(79, 129, 189), 2));

		JLabel lblChanges = new JLabel("", SwingConstants.CENTER);
		SwingUtil.addLabel(lblChanges, 10, 2, 200, 20, "Cargando imágenes", pProgress, null);
		lblChanges.setFont(new Font("Segoe UI", Font.BOLD, 12));

		pbProgress = new JProgressBar();
		pbProgress.setBackground(Color.WHITE);
		pbProgress.setBounds(10, 24, 200, 20);
		pbProgress.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
		pbProgress.setFont(new Font("Segoe UI", Font.BOLD, 12));
		pbProgress.setStringPainted(true);
		pbProgress.setForeground(new Color(79, 129, 189));

		pProgress.add(pbProgress);
		getContentPane().add(pProgress);
	}

	public void run() {
		setVisible(true);
		frmSpace.setEnabled(false);
		AWTUtilities.setWindowOpacity(frmSpace, 0.75f);

		int length = files.length;
		entryPerc = 100d / length;
		if (length > 0) {
			List<File> remainingFiles = new ArrayList<File>();
			boolean thumbExists;
			for (File file : files) {
				thumbExists = false;
				for (Thumb thumb : frmSpace.imgThumbnails) {
					try {
						if (file.getCanonicalPath().equals(thumb.imagePath)) {
							Border border;
							if (!thumb.isNew) {
								border = new EmptyBorder(0, 0, 0, 0);
							}
							else {
								border = new DashBorder(Color.BLACK, thumb, false);
							}
							thumb.label.setBorder(border);
							thumb.toDelete = false;
							thumbExists = true;
							entriesCreated++;
							setValue(0);
							break;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (!thumbExists) {
					remainingFiles.add(file);
				}
			}
			length = remainingFiles.size();
			int currentThumbSize = frmSpace.imgThumbnails.size();
			int posX = 5 + ((currentThumbSize % 5) * 105), mod;
			int posY = 1 + (80 * (int)Math.floor(currentThumbSize / 5));
			if (currentThumbSize % 5 == 0) {
				posY -= 80;
			}
			int sep;
			for (int i = currentThumbSize; i < length + currentThumbSize; i++, posX += 105) {
				try {
					final String imagePath = remainingFiles.get(i - currentThumbSize).getCanonicalPath();
					sep = imagePath.lastIndexOf(File.separator);
					final String imageName = imagePath.substring(sep + 1, imagePath.length());
					mod = i % 5;
					if (mod == 0) {
						posX = 5;
						posY += 80;
					}
					final Thumb thumb = new Thumb(new JLabel());
					thumb.isNew = true;
					frmSpace.addLabel(thumb, frmSpace.pThumbnails, frmSpace.imgThumbnails,
							posX, posY, 100, 75, imagePath, imageName, false);
					thumb.label.addMouseListener(new MouseAdapter() {
						public void mousePressed(MouseEvent e) {
							thumb.label.requestFocusInWindow();
							frmSpace.setSelectedImageThumb(thumb);
							frmSpace.paintBorder(thumb, true);
							frmSpace.showImagePreview();
						}
						public void mouseEntered(MouseEvent e) {
							frmSpace.paintBorder(thumb, true);
						}
						public void mouseExited(MouseEvent e) {
							frmSpace.paintBorder(thumb, false);
						}
					});
					thumb.label.addKeyListener(new KeyListener() {
						public void keyTyped(KeyEvent e) {
						}
						public void keyPressed(KeyEvent e) {
							switch (e.getKeyCode()) {
								case KeyEvent.VK_UP:
									frmSpace.setSelectedImageThumb(-5);
									break;
								case KeyEvent.VK_DOWN:
									frmSpace.setSelectedImageThumb(5);
									break;
								case KeyEvent.VK_RIGHT:
									frmSpace.setSelectedImageThumb(1);
									break;
								case KeyEvent.VK_LEFT:
									frmSpace.setSelectedImageThumb(-1);
									break;
							}
						}
						public void keyReleased(KeyEvent e) {
							if(e.getKeyCode() == KeyEvent.VK_DELETE) {
								frmSpace.actionDeleteImage();
							}
						}
					});
					entriesCreated++;
					setValue(0);
				} catch (Exception e) { e.printStackTrace(); }
			}
			if (FrameSpace.selectedImageThumb == null) {
				FrameSpace.selectedImageThumb = frmSpace.imgThumbnails.get(0);
			}
			frmSpace.setSelectedImageThumb(frmSpace.imgThumbnails.get(0));
			frmSpace.paintBorder(frmSpace.imgThumbnails.get(0), true);
			frmSpace.showImagePreview();
			frmSpace.pThumbnails.setPreferredSize(new Dimension
					(530, ((int)Math.ceil(frmSpace.imgThumbnails.size() / 5.0) * 80) + 1));
			frmSpace.pThumbnails.repaint();
			frmSpace.pThumbnails.revalidate();
			frmSpace.showImageButtons(true);
		}

		setVisible(false);
		dispose();
		frmSpace.setEnabled(true);
		frmSpace.toFront();
		frmSpace.addInfo();
		AWTUtilities.setWindowOpacity(frmSpace, 1f);
	}

	//! Sets the progress bar value
	private void setValue(int value){
		pbProgress.setValue((int)Math.ceil(entryPerc * entriesCreated) + value);
	}
}
